package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.Put;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/*
* 菜品管理
* */
@RestController
@Slf4j
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DishService dishService;
    /**新增菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){

        log.info("新增菜品，{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);

        //清理redis缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        //redisTemplate.delete(key);
        cleanCache(key);

        return Result.success();
    }


    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */

    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询，{}",dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);

    }


    @ApiOperation("批量删除菜品")
    @DeleteMapping
    //@RequestParam  动态解析传来的字符串，把id提取出来封装到List集合中
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除菜品，{}",ids);
        //批量删除
        dishService.deleteBatch(ids);
        //将所有的菜品缓存数据清理，所有以dish_ 开头的key
       /* Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);*/
        cleanCache("dish_*");

        return Result.success();

    }

    /*
    * 根据id查询数据用于回显*/
    @ApiOperation("根据id查询数据用于回显")
    @GetMapping("/{id}")
    public Result<DishVO> FindById(@PathVariable Long id){
        log.info("根据id查询数据用于回显,{}",id);
        DishVO dishVo = dishService.getByIdWithFlavor(id);
        return Result.success(dishVo);
    }

    /*
    * 修改菜品数据和口味信息*/
    @ApiOperation("修改菜品数据")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){

        log.info("修改菜品数据,{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);

        //将所有的菜品缓存数据清理，所有以dish_ 开头的key
        /*Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);*/

        cleanCache("dish_*");

        return Result.success();
    }


    @PostMapping("/status/{status}")
    @ApiOperation("菜品启用禁用")
    public Result startOrstop(@PathVariable Integer status, Long id){
        log.info("菜品启用禁用，{}，{}",status,id);
        dishService.startOrstop(status,id);

        //将所有的菜品缓存数据清理，所有以dish_ 开头的key
        /*Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);*/
        cleanCache("dish_*");

        return Result.success();

    }


    /*
    * 根据分类id查询菜品
    * */

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public  Result<List<Dish>> list(Long categoryId){

//        Result<List<Dish>>  为了返回 List<Dish> 类型的数据，所以这里的Result的泛型也为List<Dish>
        log.info("根据分类id查询菜品，id：{}",categoryId);
        List<Dish> dishList = dishService.list(categoryId);
        return Result.success(dishList);

    }


    //清理redis缓存的方法
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);

    }




}
